
function popupFormatWarningDialog(obj, message) {
    $("#dialog-warning-notice").html(message);
    var o = $(obj).offset();
    var t = o.top - 50;
    var l = o.left + $(obj).width() / 2 - $("#cart-format-warning-dialog").width() / 2;

    $("#cart-format-warning-dialog").css({
        "display": "block",
        "top": t,
        "left": l,
        "z-index": 1000
    });
    window.setTimeout("hideDialog('cart-format-warning-dialog')", 2000);
}


    $("#pcount").bind("change", function(event) {
        if ($(this).val() == null || $(this).val() == "") {
            popupFormatWarningDialog(this, "This is a required field!");
            return;
        }
        if (isNaN($(this).val()) || $(this).val() == null || $(this).val() == "") { //��������
            popupFormatWarningDialog(this, "You can only enter numbers in this field!");
            return;
        }
        inputSkuNum(this);
    });

function BuyUrl(wid){
    var eSize=$("#eSize").val();
    var eColor1=$("#eColor1").val();
    var eColor2=$("#eColor2").val();
    var eBust=$("#eBust").find("option:selected").text();
    var eWaist=$("#eWaist").find("option:selected").text();
    var eHips=$("#eHips").find("option:selected").text();
    var eFloor=$("#eFloor").find("option:selected").text();
    var eHeight=$("#eHeight").find("option:selected").text();
    var pcounts = $("#pcount").val();
    var patrn = /^[0-9]{1,4}$/;
    if (!patrn.test(pcounts)){
        pcounts = 1;
    }
    else {
        if (pcounts <= 0) pcounts = 1;
        if (pcounts >= 1000) pcounts = 999;
    }

    if($("#boxCustom").attr("checked")){
        var eEarameters="&skuCustomInfo.color="+eColor2+"&skuCustomInfo.bust="+eBust+"&skuCustomInfo.waist="+eWaist+"&skuCustomInfo.hips="+eHips+"&skuCustomInfo.hollowToFloor="+eFloor+"&skuCustomInfo.height="+eHeight;
        var carturl="http://cart.en.jd.com/cart/addSkuToCart.action?rd="+Math.random()+"&pid="+wid+"&pcount=1&ptype=1";
        $("#addCart a").attr("href",carturl+eEarameters);
    }
    else{
        var carturl="http://cart.en.jd.com/cart/addSkuToCart.action?rd="+Math.random()+"&pid="+wid+"&pcount="+pcounts+"&ptype=1";
        $("#addCart a").attr("href",carturl);
    }
};
//custom product show
$("#boxCustom").click(function(){
    if($("#boxCustom").attr("checked")){
        $("#sizeCustom").hide();
        $("#colorCustom").hide();
        $("#conCustom").show();
        $(".quantity1").hide();
        $(".quantity2").show();
        productPage.FN_ChangeCustomData();
    }
    else{
        $("#sizeCustom").show();
        $("#colorCustom").show();
        $("#conCustom").hide();
        $(".quantity1").show();
        $(".quantity2").hide();
        productPage.FN_InitChangeData();
    }
});

$(function(){
    $("#about .tab li").mouseover(function(){
        $("#about .tab li").removeClass("curr");
        $(this).addClass("curr");
        var conid=$(this).attr("id")+"con";
        $("#"+conid).show().siblings().hide();
    });
});

$(function(){
    $(".d-price").hover(function(){
        $(this).addClass("hover");
        $(".iframe-float").show();
        $(this).find("ul").show();
    }, function(){
        $(this).removeClass("hover");
        $(".iframe-float").hide();
        $(this).find("ul").hide();
    });
});

/*checkIndex*/
function checkIndex(obj){
	for(var i=0;i<obj.length;i++){
		if(document.getElementById(obj[i]).getAttribute("load")){
			return i
		}
	}
}

/*jqzoom*/
$(function(){
    $(".jqzoom").jqueryzoom({xzoom:350,yzoom:350,offset:10,position:"right",preload:0,lens:1});
    if($("#cloth-list").length > 0){
         $("#cloth-list").jdMarquee({deriction:"up",width:56,height:370,step:2,speed:4,delay:10,control:true,_front:"#spec-right",_back:"#spec-left"});
         $("#cloth-list img").mouseover(function() {
            var src = $(this).attr("src");
            $("#cloth-list img").removeClass('img-hover');
            $(this).addClass('img-hover');
			var str = src.substring(32,37);
			if(str=="50x65"){
            $("#spec-n1 img").eq(0).attr({
                src: src.replace("ept\/s50x65_","eptcus\/s350x450_"),
                jqimg: src.replace("s50x65_","s700x900_")
            });}else{
				$("#spec-n1 img").eq(0).attr({
				src: src.replace("\/n5\/", "\/n1\/"),
            	jqimg: src.replace("\/n5\/", "\/n0\/")
           		});
				}
        });
        $("#cloth-list img").eq(0).mouseover();
    }
    if($("#spec-list").length > 0){
        $("#spec-list").jdMarquee({deriction:"left",width:300,height:56,step:2,speed:4,delay:10,control:true,_front:"#spec-right",_back:"#spec-left"});
        $("#spec-list img").mouseover(function() {
            var src = $(this).attr("src");
            $("#spec-list img").removeClass('img-hover');
            $(this).addClass('img-hover');
			var str = src.substring(32,37);
			if(str=="50x50"){
					$("#spec-n1 img").eq(0).attr({
					src: src.replace("ept\/s50x50_","eptnor\/s350x350_"),
					jqimg: src.replace("s50x50_","s800x800_")
           	    });
				}else{
					$("#spec-n1 img").eq(0).attr({
					src: src.replace("\/n5\/", "\/n1\/"),
					jqimg: src.replace("\/n5\/", "\/n0\/")
					});
					}
        });
        $("#spec-list img").eq(0).mouseover();
    }
});

/*jqueryzoom*/
(function($){$.fn.jqueryzoom=function(options){var settings={xzoom:200,yzoom:200,offset:10,position:"right",lens:1,preload:1};if(options){$.extend(settings,options);};var noalt='';$(this).hover(function(){var imageLeft=$(this).offset().left;var imageTop=$(this).offset().top;var imageWidth=$(this).children('img').get(0).offsetWidth;var imageHeight=$(this).children('img').get(0).offsetHeight;noalt=$(this).children("img").attr("alt");var bigimage=$(this).children("img").attr("jqimg");$(this).children("img").attr("alt",'');if($("div.zoomdiv").get().length==0){$(this).after("<div class='zoomdiv'><img class='bigimg' src='"+bigimage+"'/></div>");$(this).append("<div class='jqZoomPup'>&nbsp;</div>");};if(settings.position=="right"){if(imageLeft+imageWidth+settings.offset+settings.xzoom>screen.width){leftpos=imageLeft-settings.offset-settings.xzoom;}else{leftpos=imageLeft+imageWidth+settings.offset;}}else{leftpos=imageLeft-settings.xzoom-settings.offset;if(leftpos<0){leftpos=imageLeft+imageWidth+settings.offset;}};$("div.zoomdiv").css({top:imageTop,left:leftpos});$("div.zoomdiv").width(settings.xzoom);$("div.zoomdiv").height(settings.yzoom);$("div.zoomdiv").show();if(!settings.lens){$(this).css('cursor','crosshair');};$(document.body).mousemove(function(e){mouse=new MouseEvent(e);var bigwidth=$(".bigimg").get(0).offsetWidth;var bigheight=$(".bigimg").get(0).offsetHeight;var scaley='x';var scalex='y';if(isNaN(scalex)|isNaN(scaley)){var scalex=(bigwidth/imageWidth);var scaley=(bigheight/imageHeight);$("div.jqZoomPup").width((settings.xzoom)/(scalex*1));$("div.jqZoomPup").height((settings.yzoom)/(scaley*1));if(settings.lens){$("div.jqZoomPup").css('visibility','visible');}};xpos=mouse.x-$("div.jqZoomPup").width()/2-imageLeft;ypos=mouse.y-$("div.jqZoomPup").height()/2-imageTop;if(settings.lens){xpos=(mouse.x-$("div.jqZoomPup").width()/2 < imageLeft ) ? 0 : (mouse.x + $("div.jqZoomPup").width()/2>imageWidth+imageLeft)?(imageWidth-$("div.jqZoomPup").width()-2):xpos;ypos=(mouse.y-$("div.jqZoomPup").height()/2 < imageTop ) ? 0 : (mouse.y + $("div.jqZoomPup").height()/2>imageHeight+imageTop)?(imageHeight-$("div.jqZoomPup").height()-2):ypos;};if(settings.lens){$("div.jqZoomPup").css({top:ypos,left:xpos});};scrolly=ypos;$("div.zoomdiv").get(0).scrollTop=scrolly*scaley;scrollx=xpos;$("div.zoomdiv").get(0).scrollLeft=(scrollx)*scalex;});},function(){$(this).children("img").attr("alt",noalt);$(document.body).unbind("mousemove");if(settings.lens){$("div.jqZoomPup").remove();};$("div.zoomdiv").remove();});count=0;if(settings.preload){$('body').append("<div style='display:none;' class='jqPreload"+count+"'>360buy</div>");$(this).each(function(){var imagetopreload=$(this).children("img").attr("jqimg");var content=jQuery('div.jqPreload'+count+'').html();jQuery('div.jqPreload'+count+'').html(content+'<img src=\"'+imagetopreload+'\">');});}}})(jQuery);function MouseEvent(e){this.x=e.pageX;this.y=e.pageY;}

/*EPT Product Page*/
var productPage = {
    PriceServiceUrl:"http://en.jd.com/GetCurrency/",
    StockServiceUrl:"http://st.3.cn/gsi.html",
    PreviewServiceUrl:"http://en.jd.com/GetImages/",
    S1:$("#s1"),
    S2:$("#s2"),
    P:$("#c-price"),
    O:$("#O-price"),
    ProductName:$("#name .p-name"),
    ProductId:$("#skuid"),
    FN_BindSelectOne:function(sel){ //single sale attr
        sel.change(function(){
            if (sel.val() != 0)
                productPage.FN_ChangeData(ProData[sel.val()]);
        });
    },
    FN_BindSelectAll:function(sel1,sel2){ //two sale attr
        productPage.S1.change(productPage.FN_GetSelectData);
        productPage.S2.change(productPage.FN_GetSelectData);
    },
    FN_GetSelectData:function(){ //on two sale attr get the key
        if (productPage.S1.val() != 0 && productPage.S2.val() !=0){
            var key1 = productPage.S1.val();
            var key2 = productPage.S2.val();
            if (ProData[key1+":"+key2])
                productPage.FN_ChangeData(ProData[key1+":"+key2]);
            else
                productPage.FN_DisableAddCart(); //no product disable addcart & disable click event
        }
    },
    FN_ChangeData:function(obj){ //do change
        if (obj){ //double check
            productPage.FN_GetAllPrice(obj.id); //change price anyway
            productPage.ProductName.children(":first").html(obj.name); //change product name
            productPage.ProductId.children(":first").find("span").html(obj.id); //change show skuid
            $("#name").attr("pshowskuid", obj.id); //change pshowskuid
            productPage.FN_CheckStockAndState(obj); //check stock and state
            productPage.FN_ChangePreview(obj.id); //change preview
        }
    },
    FN_ChangeCustomData:function(){//do custom change
        if (ProData[":CUSTOM"]){
            productPage.FN_ChangeData(ProData[":CUSTOM"]); //change to custom product
        }
        else
            productPage.FN_DisableAddCart(); //no product disable addcart & disable click event
    },
    FN_InitChangeData:function(){//uncheck custom event
        if (productPage.S1.val() != 0 && productPage.S2.val() != 0)
            productPage.FN_GetSelectData();
        else{
            for(var o in ProData){
                productPage.FN_ChangeData(ProData[o]);
                break;
            }
        }
    },
    FN_DisableAddCart:function(){ //disable cart button
        $("#addCart a").unbind("click").removeAttr("onclick").removeAttr("href");
        $("#addCart").removeClass("btn").addClass("btn-no");
    },
    FN_GetAllPrice:function(skuId){ //get price
        $.getJSONP(this.PriceServiceUrl + skuId + "?callback=productPage.FN_SetAllPrice" + "&" + Math.random(), productPage.FN_SetAllPrice);
    },
    FN_SetAllPrice:function(data){ //callback get price
        if (data){
            productPage.P.html(data.USD.jdPrice);
			if(data.USD.skuPrice != data.USD.jdPrice){
				productPage.O.html(data.USD.skuPrice);
				}
			//productPage.O.html(data.USD.skuPrice);

            $("#P_EUR").html(data.EUR.jdPrice);
            $("#P_GBP").html(data.GBP.jdPrice);
            $("#P_AUD").html(data.AUD.jdPrice);
            $("#P_HKD").html(data.HKD.jdPrice);

			//$("#O_EUR").html(data.EUR.skuPrice);
            //$("#O_GBP").html(data.GBP.skuPrice);
            //$("#O_AUD").html(data.AUD.skuPrice);
            //$("#O_HKD").html(data.HKD.skuPrice);
        }
    },
    FN_CheckStockAndState:function(o){ //check stock and state
        $.ajax({
            url: productPage.StockServiceUrl,
            data: "type=provincestock&provinceid=100000001&skuid=" + o.skey,
            dataType: "jsonp",
            success: function(data){
                if(data){
                    if (data.stock.StockState == 33 || data.stock.StockState == 36 || data.stock.StockState == 39 || data.stock.StockState == 40){ //check stock
                        if (o.state == 1){ //on sale
                            $("#addCart").removeClass().addClass("btn"); //change style
                            $("#addCart a").click(function(){BuyUrl(o.id);}).attr("href", "#"); //add event
                        }
                        else{ //down
                            productPage.FN_DisableAddCart();
                        }
                    }
                    else
                        productPage.FN_DisableAddCart();
                }
            }
        });
    },
    FN_ChangePreview:function(skuId){ // change product preview
        var iskuid = parseInt(skuId, 10);
        if ((iskuid < 10000000 || iskuid > 30000000) && !ProData[":CUSTOM"]) // check can change preview
            $.getJSONP(this.PreviewServiceUrl + skuId + "?callback=productPage.FN_SetPreview", productPage.FN_SetPreview);
    },
    FN_SetPreview:function(data){ // callback change product preview
        if (data){
            if($("#cloth-list").length > 0){
                productPage.FN_PreviewMan("#cloth-list", data);

            }
            else if($("#spec-list").length > 0){
                productPage.FN_PreviewMan("#spec-list", data);

            }
        }
    },
    FN_PreviewMan:function(str, data){
        if(str && data){
            productPage.FN_InitMainPreview(data[0]); //init main preview
            //start init preview list
            var simgwarp = PreviewFilter($(str + " img").eq(0).attr("src"));
            $(str + " img").unbind("mouseover");
            var base = $(str + " ul.list-h li").eq(0);
            base.find("img").removeClass('img-hover');
            $(str + " ul.list-h").attr("style", "").empty().appendTo(str);
            $(str + " div").remove();
            if (data.length == 1 && $(str + " ul.list-h").size() == 2) $(str + " ul.list-h").eq(1).remove();
            for (var i = 0; i < data.length; i++){
                if(data[i]){
                    base.find("img").attr("src", simgwarp + "_" + data[i]);
                    base.clone().appendTo(str + " ul.list-h");
                }
            }
            $(str + " img").eq(0).addClass('img-hover'); //init one
            // end
            // bind list preview event
            $(str + " img").livequery("mouseover",function(){
                var src = $(this).attr("src");
                $(str + " img").removeClass('img-hover');
                $(this).addClass('img-hover');
                productPage.FN_InitMainPreview(src.substring(src.indexOf("_") + 1));
            });
        }
    },
    FN_InitMainPreview:function(mainsrc){
        var strHead = "#spec-n1";
        var bimgwarp = PreviewFilter($(strHead + " img").attr("src"));
        var jimgwarp = PreviewFilter($(strHead + " img").attr("jqimg"));
        $(strHead + " img").attr({
            src: bimgwarp + "_" + mainsrc,
            jqimg: jimgwarp + "_" + mainsrc
        });
    },
    Init:function(){
        var sid = $("#name").attr("pshowskuid"); //skuid ident
        if (sid) this.FN_GetAllPrice(sid);  //price
        if (ProData) { //check ProData
            if (ProData.id)
                productPage.FN_CheckStockAndState(ProData);
            else {
                for (var o in ProData){
                    if (ProData.hasOwnProperty(o))
                    {
                        if (ProData[o].id == sid){
                            productPage.FN_CheckStockAndState(ProData[o]);
                        }
                    }
                }
            }
            //one sale attr or two
            var S1 = productPage.S1;
            var S2 = productPage.S2;
            if (S1.length !=0 && S2.length != 0)
                productPage.FN_BindSelectAll(S1, S2);
            else if (S1.length > 0)
                productPage.FN_BindSelectOne(S1);
            else if (S2.length > 0)
                productPage.FN_BindSelectOne(S2);
        }
    }
};
productPage.Init();

/*EPT Customer Review*/
var customerReview = {
    ReviewServiceUrl:"http://club.en.jd.com/java/reviews/summary/getReviewsByPage",
    TPL_Reviews:"{for item in reviewsList}\
                    <div class=\"item\" itemprop=\"review\" itemscope itemtype=\"http://schema.org/Review\">\
                        <div class=\"user\">\
                            <div class=\"u-name\"><a href=\"#\" target=\"_blank\" itemprop=\"author\">${item.reviewerName}</a></div>\
                            <div class=\"u-icon\"><a href=\"#\" target=\"_blank\"><img src=\"http://static.360buyimg.com/ept/product/img/rev-head.png\" width=\"50\" height=\"50\" /></a></div>\
                            <div class=\"date-buy\">${item.created}</div>\
							<meta content=${(item.created).substring(0,10)} itemprop=\"datePublished\" >\
                        </div>\
                        <div class=\"i-item\">\
                            <div class=\"comment-content\" itemprop=\"description\">${item.reviewContent}</div>\
                        </div>\
                    </div>\
                {/for}",
    Init_Reviews:function(skuId, page){
        $.ajax({
            url: customerReview.ReviewServiceUrl,
            data: "productId=" + skuId + "&page=" + page,
            dataType: "jsonp",
            success: function(data){
                if(data){
                    var html = customerReview.TPL_Reviews.process(data);
                    $("#comment .mc .see").before(html);
                }
            }
        });

    },
	Init_Count:function(skuId){
		$.getJSON(
			customerReview.ReviewServiceUrl+"?productId=" + skuId+"&callback=?",
			function(data) {
				$(".reviews_itemprop").attr("content",data.countAll);
				if(data.countAll > 0){
					$("#commStar").show()
					$(".reviews").html('('+data.countAll+')');
				}else{
					$("#commStar1").hide();
					$("#commAverage").html("There are no customer reviews yet. ");
				}

			}
		);
		//alert(customerReview.ReviewServiceUrl+"?productId=" + skuId+"&callback=?")
		},
    Init:function(){
        this.Init_Reviews($("#name").attr("pshowskuid"), 1);
		this.Init_Count($("#name").attr("pshowskuid"));
    }
};
customerReview.Init();
/*EPT Wish List*/
var wishList = {
    WishListServiceUrl:"http://club.en.jd.com/java/myWishlist/expand/addMyWishlist",
    FN_AddWishList:function(skuId){
        $.ajax({
            url: wishList.WishListServiceUrl,
            data: "productId=" + skuId,
            dataType: "jsonp",
            success: function(data){
                if(data){
                    if (data.resultCode == 1)
                        alert("Item has been added to Wish List!");
                    else if (data.resultCode == 4)
                        alert("Please login to add to Wish List!");
                    else if (data.resultCode == 3)
                        alert("This item has already been added to your Wish List!");
                    else
                        alert("There was a problem adding this item to your Wish List. Please try again later!");
                }
            }
        });
    },
    Init:function(){
        $("#summary .btn").click(function(){
            wishList.FN_AddWishList($("#name").attr("pshowskuid"));
        });
    }
};
wishList.Init();
/*Useful function*/
function PreviewFilter(str){
    var result = "";
    if(str){
        if (IsJdOrEpt()){
            //undo
        }
        result = str.split("_")[0];
    }
    return result;
};
function IsJdOrEpt(){
    //Jd: true  Ept: false
    var s = parseInt($("#name").attr("pshowskuid"), 10);
    if (s > 600000000 && s < 700000000)
        return false;
    return true;
};
